<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Calendar Class
 *
 * This class enables the creation of calendars
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/calendar.html
 */
class Captcha {
	
	var $CI;
	var $code_str        = array(
							'1'=>'23456789',
							'2'=>'abcdefghgkmnpqrstwxyzABCDEFGHGKMNPQRSTWXYZ',
							'4'=>'路阳光子无开月了的比小山古天中石头井火大西木开人也叶九七六五合文只个鱼羊哈么点四生件格不步布各你田红蓝来很春下画京左百白东关工共足方尺才梦见查着去区曲寻早可上丰爱呵刘谷');
	var $code            = '';
	var $code_arr        = array();
	var $code_type       = 1;
	var $code_length     = 5;
	var $captcha         = '';
	var $width           = 75;
	var $height          = 23;
	var $image;
	var $font_path       = '/system/fonts/';
	var $font_name       = 'simhei.ttf';
	var $session_name    = 'captcha';
	var $color_backgroud = array('r' => 0, 'g' => 0, 'b' => 0);
	var $color_font      = array('r' => 255, 'g' => 255, 'b' => 255);
	var $color_line      = array('r' => 200, 'g' => 200, 'b' => 200);
	var $color_dot       = array('r' => 200, 'g' => 200, 'b' => 200);
	
	public function __construct($params = array())
    {
    	$this->CI =& get_instance();

		$this->CI->load->library('session');
		//print_r($params);exit;
		foreach (array('width', 'height', 'font_name', 'code_type', 'code_length', 'session_name', 'color_backgroud', 'color_font', 'color_line', 'color_dot') as $key)
		{
			if (isset($params[$key]))
			{
				$this->$key = $params[$key];
			}
		}
		
		// 设置字体路径
		$this->font_path .= $this->font_name;
		$this->font_path = realpath(dirname(dirname(dirname(__FILE__)))).$this->font_path;
		//echo $this->font_path;exit;
//		if (file_exists($this->font_path))
//		{
//			echo 'yes';
//		}
//		exit;
		
    }
    
    public function set_code_str()
    {
    	$this->code_type = intval($this->code_type);
    	$this->code_type = $this->code_type > 0 ? $this->code_type : 1 ;
    	foreach ($this->code_str as $key => $value)
    	{
    		if (($key & $this->code_type) === $key)
    		{
    			$this->code .= $value;
    		}
    	}
    }
    
    public function create_code()
    {
    	$this->code_length = intval($this->code_length);
    	preg_match_all("/./u", $this->code, $ar);
    	$code_len=count($ar['0']);
    	$code_array=$ar['0'];
    	//$code_arr='';
    	//$this->captcha['captcha'] = '';
    	for($i=0 ; $i < $this->code_length ; $i++){
    		$this->code_arr[$i]  = $code_array[rand(1,$code_len)-1];
    		$this->captcha = $this->captcha . $this->code_arr[$i];
    	}
    	$this->captcha = strtolower($this->captcha);
    }
    
    function show()
    {
    	// 根据要显示的验证码字符串类型组合验证码字符串
    	$this->set_code_str();

    	// 生成随机的验证码字符
    	$this->create_code();

    	// 将验证码存入数据库
    	$this->CI->session->set_userdata($this->session_name, $this->captcha);
    	//echo $this->captcha;exit;

    	// 创建图像，并设置大小
    	$this->image= imagecreatetruecolor($this->width, $this->height);

    	// 图像背景颜色
    	$c_b=imagecolorallocate($this->image, $this->color_backgroud['r'], $this->color_backgroud['g'], $this->color_backgroud['b']);

    	// 图像中文字颜色
    	$c_f=imagecolorallocate($this->image, $this->color_font['r'], $this->color_font['g'], $this->color_font['b']);

    	// 图像中干扰线颜色
    	$c_l=imagecolorallocate($this->image, $this->color_line['r'], $this->color_line['g'], $this->color_line['b']);

    	// 图像躁点颜色
    	$c_d=imagecolorallocate($this->image, $this->color_dot['r'], $this->color_dot['g'], $this->color_dot['b']);
    	
    	// 将字符写在图像上
    	$position_x=rand(1, 35);
    	$position_y=rand(0, 6);
    	//添加干扰线
    	$l_num=2;
    	for ($j = 0; $j < $l_num; $j++) {
    		$l_x1=rand(0, $position_x-10);
    		$l_y1=rand(0, $position_y+10);
    		$l_x2=rand($position_x+40,$position_x+60);
    		$l_y2=rand($position_y+5, 23);
    		imageline($this->image, $l_x1, $l_y1,$l_x2,$l_y2,$c_l);
    	}
    	//添加躁点
    	$dot_num=150;
    	for ($j = 0; $j < $dot_num; $j++) {
    		imagesetpixel($this->image, rand()%100, rand()%30, $c_d);
    	}
    	//每个字的偏移量
    	$str_l=15;
    	for ($j2 = 0; $j2 < count($this->code_arr); $j2++) {
    		$x=rand(-1, 2)+$str_l*$j2;
    		$y=rand(14, 22);
    		$str=iconv('utf-8', 'utf-8', $this->code_arr[$j2]);
    		imagettftext($this->image, 12, 0, $x, $y, $c_f, $this->font_path,$str);
    	}
    	//imagestring($this->image,6, $position_x, $position_y, $code, $c_f);
    	header("Content-type:image/jpeg");
    	imagejpeg($this->image);
    }
}

// END CI_Calendar class

/* End of file Calendar.php */
/* Location: ./system/libraries/Calendar.php */